﻿using CodeNotes.BLL;
using CodeNotes.Model;
using Maticsoft.DBUtility;
using MvcCodeNotes.Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;

namespace MvcCodeNotes.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index(int? page, string keyword)
        {
            ViewBag.Message = "个人博客";
            int pageSize = 15;
            int pageIndex = page == null ? 0 : Convert.ToInt32(page);
            string sqlStr = "1=1";
            if (!String.IsNullOrEmpty(keyword))
            {
                sqlStr = string.Format("(Title like '%{0}%' or Abstract like '%{0}%')", keyword);
            }
            sqlStr += " order by ArticleId desc";

            List<CW_Article> listTemp = new CW_ArticleLogic().GetModelList(sqlStr);

            List<CodeNotes.Model.CW_Menu> menuList = new CW_MenuLogic().GetModelList("");
            List<CW_ArticleView> list = new List<CW_ArticleView>();
            foreach (CW_Article a in listTemp)
            {
                CW_ArticleView av = new CW_ArticleView();
                av.Article = a;
                if (a.MenuId > 0)
                {
                    CW_Menu menu = menuList.Where(p => p.MenuId == a.MenuId).FirstOrDefault();
                    if (menu != null)
                        av.MenuName = menu.MenuName;
                }
                list.Add(av);
            }

            Pagination<CW_ArticleView> paginateNew = new Pagination<CW_ArticleView>(list, pageIndex, pageSize);


            return View(paginateNew);
        }

        public ActionResult List()
        {
            ViewBag.Message = "这是List.";

            return View();
        }


        public ActionResult Detail(int? id)
        {
            CW_ArticleView av = new CW_ArticleView();
            ViewBag.Message = "这是Detail.";
            CW_Article article = null;
            if (id > 0)
            {
                article = new CW_ArticleLogic().GetModel((int)id);
            }
            if (article == null)
            {
                return RedirectToAction("Index");
            }
            else
            {

                av.Article = article;
                if (article.MenuId > 0)
                {
                    av.MenuName = new CW_MenuLogic().GetModel((int)article.MenuId).MenuName;
                }
                List<CW_Tag> articleTag = new CW_TagLogic().GetModelList("TagId in (select TagId from CW_TagRelate where ArticleId=" + id + ")");
                av.tagNames = articleTag;

                article.PV += 1;
                new CW_ArticleLogic().Update(article);

            }

            return View(av);
        }

        public ActionResult ArticleType(int menuid, int? page)
        {
            CW_Menu menu = new CW_MenuLogic().GetModel(menuid);
            ViewBag.MenuName = menu.MenuName;
            if(menu.MenuType=="link")
            {
                return Redirect(menu.Url);
            }
            else if (menu.MenuType == "parentmenu")
            {
                List<CW_Menu> subMenulist = new CW_MenuLogic().GetModelList("ParentId="+menu.MenuId+" order by Sort");
                return View("MenuList", subMenulist);
            }
            else
            {
                int pageSize = 15;
                int pageIndex = page == null ? 0 : Convert.ToInt32(page);
                string sqlStr = "1=1";
                if (menuid > 0)
                {
                    sqlStr = " MenuId=" + menuid;
                }
                sqlStr += " order by ArticleId desc";
                List<CW_Article> listTemp = new CW_ArticleLogic().GetModelList(sqlStr);

                List<CW_ArticleView> list = new List<CW_ArticleView>();
                foreach (CW_Article a in listTemp)
                {
                    CW_ArticleView av = new CW_ArticleView();
                    av.Article = a;
                    av.MenuName = menu.MenuName;
                    list.Add(av);
                }

                Pagination<CW_ArticleView> paginateNew = new Pagination<CW_ArticleView>(list, pageIndex, pageSize);


                return View(paginateNew);
            }

           
        }

        public ActionResult Tag(int tagid, int? page)
        {
            CW_Tag tag=new CW_TagLogic().GetModel(tagid);
            if(tag==null)
            {
                return RedirectToAction("index");
            }
            ViewBag.TagName = tag.TagName;
            int pageSize = 15;
            int pageIndex = page == null ? 0 : Convert.ToInt32(page);
            string sqlStr = "IsShow=1";
            if (tagid > 0)
            {
                sqlStr += "  and ArticleId in (select ArticleId from dbo.CW_TagRelate where TagId="+tagid+")";
            }
            sqlStr += " order by ArticleId desc";
            List<CW_Article> listTemp = new CW_ArticleLogic().GetModelList(sqlStr);

            List<CodeNotes.Model.CW_Menu> menuList = new CW_MenuLogic().GetModelList("");
            List<CW_ArticleView> list = new List<CW_ArticleView>();
            foreach (CW_Article a in listTemp)
            {
                var menu = menuList.Where(m => m.MenuId == a.MenuId).FirstOrDefault();
                CW_ArticleView av = new CW_ArticleView();
                av.Article = a;
                if(menu!=null)
                    av.MenuName = menu.MenuName;
                list.Add(av);
            }

            Pagination<CW_ArticleView> paginateNew = new Pagination<CW_ArticleView>(list, pageIndex, pageSize);


            return View(paginateNew);
        }

        public ActionResult Search(int? page, string keyword)
        {
            ViewBag.Searchword = keyword;            
            int pageSize = 15;
            int pageIndex = page == null ? 0 : Convert.ToInt32(page);

            List<CW_Article> listTemp = new CW_ArticleLogic().GetModelListByKeyword(keyword);
            if(listTemp.Count>0)
            {
                List<CodeNotes.Model.CW_Menu> menuList = new CW_MenuLogic().GetModelList("");
                List<CW_ArticleView> list = new List<CW_ArticleView>();
                foreach (CW_Article a in listTemp)
                {
                    CW_ArticleView av = new CW_ArticleView();
                    av.Article = a;
                    if (a.MenuId > 0)
                    {
                        CW_Menu menu = menuList.Where(p => p.MenuId == a.MenuId).FirstOrDefault();
                        if (menu != null)
                            av.MenuName = menu.MenuName;
                    }
                    list.Add(av);
                }
                Pagination<CW_ArticleView> paginateNew = new Pagination<CW_ArticleView>(list, pageIndex, pageSize);
                return View(paginateNew);
            }
            else
            {
                return View("Notfound");
            }

            
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your app description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }

        //
        // GET: /Store/GenreMenu

        [ChildActionOnly]
        public ActionResult FooterMenu()
        {
            CW_Menu menu = new CW_Menu
            {
                MenuId = 1,
                MenuName = "Index菜单管理",
                Url = "Menu"
            };
            CW_Menu menu2 = new CW_Menu
            {
                MenuId = 2,
                MenuName = "Index文章管理",
                Url = "Article"
            };
            CW_Menu menu3 = new CW_Menu
            {
                MenuId = 3,
                MenuName = "Index退出登录",
                Url = "Login"
            };

            List<CW_Menu> list = new List<CW_Menu>();
            list.Add(menu);
            list.Add(menu2);
            list.Add(menu3);
            return PartialView(list);
        }

        [ChildActionOnly]
        public ActionResult SiteMenu()
        {
            List<CW_Menu> list = new CW_MenuLogic().GetModelList("IsShow=1 and (ParentId=0 or ParentId is null)");
            List<CW_MenuView> menuList = new List<CW_MenuView>();
            foreach (var item in list)
            {
                CW_MenuView mv = new CW_MenuView();
                mv.menu = item;

                List<CW_Menu> child = new CW_MenuLogic().GetModelList("IsShow=1 and ParentId=" + item.MenuId);
                mv.ChildList = child;
                menuList.Add(mv);
            }
            return PartialView(menuList);
        }

        [ChildActionOnly]
        public ActionResult AsidePartial()
        {
            List<CW_Article> articleList = new CW_ArticleLogic().GetModelList("IsShow=1");
            IEnumerable<CW_Article>articleTop10= articleList.Take(10);
            List<CW_Tag> tagList = new CW_TagLogic().GetModelList("");
            ViewBag.tagList = tagList;
            ViewBag.articleList = articleTop10;
            return PartialView();
        }
    }
}
